# ==================== 全局配置开关 ====================
import logging

logging.basicConfig(
    level=logging.DEBUG,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
    handlers=[
        logging.StreamHandler(),
        logging.FileHandler("app.log", encoding="utf-8")
    ]
)

logger = logging.getLogger(__name__)

# 图表显示配置
class ChartConfig:
    """图表全局配置类"""
    
    # 窗口配置
    DEFAULT_WINDOW_WIDTH = 1000
    DEFAULT_WINDOW_HEIGHT = 800
    DEFAULT_WINDOW_TITLE = "K线图与成交量 - PyQtGraph"
    
    # K线图配置
    CANDLESTICK_WIDTH = 0.4
    PRICE_PADDING = 0.2  # 价格范围边距比例
    CANDLESTICK_HEIGHT_FACTOR = 0.5  # 实体高度因子
    
    # 成交量图配置
    VOLUME_SCALE_FACTOR = 0.3  # 成交量缩放因子
    
    # 移动平均线配置
    DEFAULT_MA_PERIODS = [5, 10, 20, 30]
    MA_COLORS = {5: 'y', 10: 'm', 20: 'b', 30: 'r'}

    # 移动平均线斜率峰值配置 拐点
    DEFAULT_MA_SLOPE_PEAKS_PERIODS = [10]
    MA_SLOPE_PEAKS_COLORS = {10: 'y'}
    
    # 颜色配置
    RISE_COLOR = 'r'  # 上涨颜色（红色）
    FALL_COLOR = 'g'  # 下跌颜色（绿色）
    DOJI_COLOR = 'y'  # 十字星颜色（灰色）
    
    # 网格配置
    SHOW_GRID_X = True
    SHOW_GRID_Y = True
    
    # 日期格式配置
    DEFAULT_DATE_FORMAT = '%Y%m%d'
    MAX_DATE_TICKS = 10
    
    # 鼠标交互配置
    ENABLE_MOUSE_INTERACTION = True
    SHOW_TOOLTIP = True
    
    # 调试配置
    DEBUG_MODE = False
    VERBOSE_LOGGING = False

# 功能开关
class FeatureFlags:
    """功能开关类"""
    
    # 核心功能开关
    ENABLE_KLINE_CHART = True # K线图
    ENABLE_VOLUME_CHART = True # 成交量图
    ENABLE_MOVING_AVERAGE = True # 移动平均线
    ENABLE_MOVING_AVERAGE_SLOPE_PEAKS = True # 移动平均线斜率峰值
    ENABLE_MOUSE_INTERACTION = True # 鼠标交互
    ENABLE_MULTI_WINDOW = True # 多窗口
    
    # 高级功能开关
    ENABLE_ZOOM_PAN = True # 缩放和平移
    ENABLE_CROSSHAIR = True # 十字线
    ENABLE_TOOLTIP = True # 工具提示
    ENABLE_DATA_EXPORT = False # 数据导出
    ENABLE_TECHNICAL_INDICATORS = False # 技术指标
    
    # 性能优化开关
    ENABLE_DATA_CACHING = True # 数据缓存
    ENABLE_LAZY_LOADING = False # 懒加载
    ENABLE_GPU_ACCELERATION = False # GPU加速
    
    # 开发调试开关
    ENABLE_DEBUG_INFO = False # 调试信息
    ENABLE_PERFORMANCE_MONITORING = False # 性能监控
    ENABLE_ERROR_REPORTING = False # 错误报告

# 全局配置实例
config = ChartConfig()
flags = FeatureFlags()

# 便捷的配置修改函数
def set_window_size(width: int, height: int):
    """设置默认窗口大小"""
    config.DEFAULT_WINDOW_WIDTH = width
    config.DEFAULT_WINDOW_HEIGHT = height

def set_candlestick_width(width: float):
    """设置K线宽度"""
    config.CANDLESTICK_WIDTH = width

def set_color_scheme(rise_color: str, fall_color: str):
    """设置颜色方案"""
    config.RISE_COLOR = rise_color
    config.FALL_COLOR = fall_color

def enable_feature(feature_name: str, enabled: bool = True):
    """启用或禁用功能"""
    if hasattr(flags, feature_name):
        setattr(flags, feature_name, enabled)
    else:
        print(f"警告: 功能 '{feature_name}' 不存在")

def set_debug_mode(enabled: bool):
    """设置调试模式"""
    flags.ENABLE_DEBUG_INFO = enabled
    config.DEBUG_MODE = enabled
    config.VERBOSE_LOGGING = enabled

def get_config():
    """获取当前配置"""
    return config

def get_flags():
    """获取当前功能开关状态"""
    return flags

def print_config():
    """打印当前配置"""
    print("=== 图表配置 ===")
    for attr in dir(config):
        if not attr.startswith('_'):
            print(f"{attr}: {getattr(config, attr)}")
    
    print("\n=== 功能开关 ===")
    for attr in dir(flags):
        if not attr.startswith('_'):
            print(f"{attr}: {getattr(flags, attr)}")
